var app=getApp()
// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //是否显示下面的购物车
    HZL_isCat: 0,
    
    
    //分类的数组
    HZL_categories: [
      '菜单'
    ],
    //swiper滑动的数组
    HZL_swiper_ID: 0,
   
    
    // 左 => 右联动 右scroll-into-view 所需的id
    HZL_toView: null,
    // 当前左侧选择的
    HZL_currentLeftSelect: null,
    // 右侧每类数据到顶部的距离（用来与 右 => 左 联动时监听右侧滚动到顶部的距离比较）
    HZL_eachRightItemToTop: [],
    HZL_leftToTop: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.searchbar=this.selectComponent("#searchbar");
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      HZL_num: app.globalData.HZL_num,
      HZL_my_cat: app.globalData.HZL_my_cat
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  search: function(e){
    this.setData({
      goods: e.detail.goods,
      
    })
  },

  //是否显示下面的购物车
  HZL_isCat: function (e) {
    var that = this;
    if (that.data.HZL_isCat == 0 && that.data.HZL_num > 0) {
      that.setData({
        HZL_isCat: 1
      })
    } else if (that.data.HZL_isCat == 1 && that.data.HZL_num > 0) {
      that.setData({
        HZL_isCat: 0
      })
    }
  },

  //关闭
  HZL_isCat_close: function (e) {
    this.setData({
      HZL_isCat: 0
    })
  },

  //清空
  HZL_zero: function (e) {
    for (var i = 0; i < this.data.goods.length; i++) {
      for (var j = 0; j < this.data.goods[i].wxSpecies.length; j++) {
        this.data.goods[i].wxSpecies[j].wxSpeciesCount = 0
      }
    }
    this.setData({
      HZL_isCat: 0,
      HZL_num: 0,
      HZL_my_cat: [],
      goods: this.data.goods,
    })
  },

  // 增加
  HZL_jia: function (e) {
    var index = e.currentTarget.dataset.index;
    var parentIndex = e.currentTarget.dataset.parentindex;
    
    var HZL_my_cat = this.HZL_my_jia(parentIndex, index)
    this.setData({
      HZL_num: this.data.HZL_num,
      HZL_my_cat: HZL_my_cat,
      goods: this.data.goods,
    })
  },

  //减少
  HZL_jian: function (e) {
    var index = e.currentTarget.dataset.index;
    var parentIndex = e.currentTarget.dataset.parentindex;
    var HZL_my_cat = this.HZL_my_jian(parentIndex, index)

    if (this.data.HZL_num == 0) {
      this.data.HZL_isCat = 0;
    } else {
      this.data.HZL_isCat = 1;
    }

    this.setData({
      HZL_num: this.data.HZL_num,
      HZL_my_cat: HZL_my_cat,
      goods: this.data.goods,
    })
  },

  // 下面购物车增加
  HZL_jia1: function (e) {
    var index = e.currentTarget.dataset.index;
    var parentIndex = e.currentTarget.dataset.parentindex;
    var HZL_my_cat = this.HZL_my_jia(parentIndex, index)
    this.setData({
      HZL_num: this.data.HZL_num,
      HZL_my_cat: HZL_my_cat,
      goods: this.data.goods,
    })
  },

  //下面购物车减少
  HZL_jian1: function (e) {
    var index = e.currentTarget.dataset.index;
    var parentIndex = e.currentTarget.dataset.parentindex;
    var HZL_my_cat = this.HZL_my_jian(parentIndex, index)

    if (this.data.HZL_num == 0) {
      this.data.HZL_isCat = 0;
    } else {
      this.data.HZL_isCat = 1;
    }

    this.setData({
      HZL_num: this.data.HZL_num,
      HZL_my_cat: HZL_my_cat,
      goods: this.data.goods,
      HZL_isCat: this.data.HZL_isCat
    })
  },

  //封装加的方法
  HZL_my_jia: function (parentIndex, index) {
    this.data.HZL_num++;
    var index = index;
    var parentIndex = parentIndex;
    var id = this.data.goods[parentIndex].wxSpecies[index].wxSpeciesId;
    var name = this.data.goods[parentIndex].wxSpecies[index].wxSpeciesName;
    var price = this.data.goods[parentIndex].wxSpecies[index].wxSpeciesPrice;
    var num = ++this.data.goods[parentIndex].wxSpecies[index].wxSpeciesCount;
    //弄一个元素判断会不会是重复的
    var mark = 'a' + index + 'b' + parentIndex + 'c' + '0' + 'd' + '0'
    var obj = { num: num, name: name, mark: mark, index: index, parentIndex: parentIndex, price: price, id: id };
    var HZL_my_cat = this.data.HZL_my_cat;
    HZL_my_cat.push(obj)

    var arr = [];
    //去掉重复的
    for (var i = 0; i < HZL_my_cat.length; i++) {
      if (obj.name == HZL_my_cat[i].name) {
        HZL_my_cat.splice(i, 1, obj)
      }
      if (arr.indexOf(HZL_my_cat[i]) == -1) {
        arr.push(HZL_my_cat[i]);
      }
    }
    app.globalData.HZL_num = this.data.HZL_num;
    app.globalData.HZL_my_cat = this.data.HZL_my_cat;
    return arr
  },

  //封装减的方法
  HZL_my_jian: function (parentIndex, index) {
    this.data.HZL_num--;
    var index = index;
    var parentIndex = parentIndex;
    var id = this.data.goods[parentIndex].wxSpecies[index].wxSpeciesId;
    var name = this.data.goods[parentIndex].wxSpecies[index].wxSpeciesName;
    var price = this.data.goods[parentIndex].wxSpecies[index].wxSpeciesPrice;
    var num = --this.data.goods[parentIndex].wxSpecies[index].wxSpeciesCount;
    //弄一个元素判断会不会是重复的
    var mark = 'a' + index + 'b' + parentIndex + 'c' + '0' + 'd' + '0'
    var obj = { num: num, name: name, mark: mark, index: index, parentIndex: parentIndex, price: price, id: id };
    var HZL_my_cat = this.data.HZL_my_cat;
    HZL_my_cat.push(obj)

    var arr = [];
    //去掉重复的
    for (var i = 0; i < HZL_my_cat.length; i++) {
      if (obj.name == HZL_my_cat[i].name) {
        HZL_my_cat.splice(i, 1, obj)
      }
    }


    var arr1 = [];
    //当数量大于1的时候加
    for (var i = 0; i < HZL_my_cat.length; i++) {
      if (arr1.indexOf(HZL_my_cat[i]) == -1) {
        arr1.push(HZL_my_cat[i]);
        if (HZL_my_cat[i].num > 0) {
          arr.push(arr1[i])
        }
      }
    }

    app.globalData.HZL_num = this.data.HZL_num;
    app.globalData.HZL_my_cat = this.data.HZL_my_cat;

    return arr
  },
  Settlement: function (e) {
    var arr = [];
    //去掉重复的
    for (var i = 0; i < this.data.HZL_my_cat.length; i++) {
      for (var y = 0; y < this.data.HZL_my_cat.length; y++) {
        if (this.data.HZL_my_cat[y].name == this.data.HZL_my_cat[i].name) {
        this.data.HZL_my_cat.splice(y, 1, this.data.HZL_my_cat[y]);
      }
      if (arr.indexOf(this.data.HZL_my_cat[i]) == -1) {
        arr.push(this.data.HZL_my_cat[i]);
      }
    }}
    wx.navigateTo({
      url: '../../components/settlement/settlement?goods=' + JSON.stringify(arr)
    });
  }
})